﻿using System;
using System.Collections.Generic;

namespace Fetches.Fetch.Utils.FileHosting
{

    public class FileTypeHelper
    {
        private static Dictionary<int, string> _knownTypeDictionary = new Dictionary<int, string>();

        static FileTypeHelper()
        {
            _knownTypeDictionary[255216] = "JPG";
            _knownTypeDictionary[7173] = "GIF";
            _knownTypeDictionary[6677] = "BMP";
            _knownTypeDictionary[13780] = "PNG";
            _knownTypeDictionary[7790] = "COM";
            _knownTypeDictionary[7790] = "EXE";
            _knownTypeDictionary[7790] = "DLL";
            _knownTypeDictionary[8297] = "RAR";
            _knownTypeDictionary[8075] = "ZIP";
            _knownTypeDictionary[6063] = "XML";
            _knownTypeDictionary[6033] = "HTML";
            _knownTypeDictionary[239187] = "ASPX";
            _knownTypeDictionary[117115] = "CS";
            _knownTypeDictionary[119105] = "JS";
            _knownTypeDictionary[210187] = "TXT";
            _knownTypeDictionary[255254] = "SQL";
            _knownTypeDictionary[64101] = "BAT";
            _knownTypeDictionary[10056] = "BTSEED";
            _knownTypeDictionary[255254] = "RDP";
            _knownTypeDictionary[5666] = "PSD";
            _knownTypeDictionary[3780] = "PDF";
            _knownTypeDictionary[7384] = "CHM";
            _knownTypeDictionary[70105] = "LOG";
            _knownTypeDictionary[8269] = "REG";
            _knownTypeDictionary[6395] = "HLP";
            _knownTypeDictionary[208207] = "DOC";
            _knownTypeDictionary[208207] = "XLS";
            _knownTypeDictionary[208207] = "DOCX";
            _knownTypeDictionary[208207] = "XLSX";
        }

        public static string GetRealTypeCode(string path)
        {
            System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fileStream);
            string realType = " ";
            byte buffer;
            try
            {
                buffer = binaryReader.ReadByte();
                realType = buffer.ToString();
                buffer = binaryReader.ReadByte();
                realType += buffer.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            binaryReader.Close();
            fileStream.Close();
            return realType;
        }

        public static string GetRealType(string path)
        {
            var code = GetRealTypeCode(path);
            int codeValue = Convert.ToInt32(code);
            if (_knownTypeDictionary.ContainsKey(codeValue))
            {
                return _knownTypeDictionary[codeValue];
            }

            return "unknown";
        }
        public static FileType Parse(string typeName)
        {
            FileType type;
            switch (typeName.ToLower())
            {
                case "text":
                    type = FileType.Text;
                    break;
                case "image":
                    type = FileType.Image;
                    break;
                case "audio":
                    type = FileType.Audio;
                    break;
                case "video":
                    type = FileType.Video;
                    break;
                case "application":
                    type = FileType.Application;
                    break;
                default:
                    type = FileType.Unknown;
                    break;
            }
            return type;
        }

        private static readonly List<FileType> HostFileTypes=new List<FileType>{FileType.Application,FileType.Audio,FileType.Image,FileType.Unknown,FileType.Text,FileType.Video};

        public static IReadOnlyList<FileType> GetAllFileTypes()
        {
            return HostFileTypes.AsReadOnly();
        }
    }
}
